package iso3.pt.action;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import iso3.pt.model.Alumno;
import iso3.pt.model.Asignatura;
import iso3.pt.model.Profesor;
import iso3.pt.service.PtDaoService;

public class LecturerAction extends com.opensymphony.xwork2.ActionSupport implements com.opensymphony.xwork2.Preparable {

	private static final long serialVersionUID = 1L;
	
	private int subjectID;
	private String subjectName;
	private Profesor profesor;
	private int studentDNI;
	private List<Asignatura> subjectList;
	private List<Alumno> studentList;

	public String execute() throws Exception {		
		return SUCCESS;
	}
	
	@SuppressWarnings({ "rawtypes" })
	@Override
	public void prepare() throws Exception {
		Map session = ActionContext.getContext().getSession();
		profesor = (Profesor) session.get("user");
		PtDaoService dao = new PtDaoService();
		subjectList = new ArrayList<Asignatura>();
		Set<Asignatura> as = dao.getAsignaturasProfesor(profesor.getId());
		
		for (Asignatura asig: as) {
			subjectList.add(asig);
		}
	}
	
	public String listStudents() {
		studentList = new ArrayList<Alumno>();
		PtDaoService dao = new PtDaoService();
		subjectName = dao.getAsignatura(subjectID).getNombre();
		
		try {
			if(profesor != null) {	
				for (Alumno alumn : dao.getAlumnos(subjectID))
					studentList.add(alumn);
			}
		} catch(Exception e) {
			System.out.println(e.getMessage());
		}
		
		return "studentList";
	}
	
	public String addMark() {
		return "addMark";
	}
	
	public String cancelStudentList() {
		return "cancelStudentList";
	}
	
	public String logout()
	{
		@SuppressWarnings("rawtypes")
		Map session = ActionContext.getContext().getSession();
	  
	  	if (session.get("user") != null)
	  		session.remove("user"); 
		
		return "logout";
	}
	

	public int getSubjectID() {
		return subjectID;
	}

	public void setSubjectID(int subjectID) {
		this.subjectID = subjectID;
	}

	public String getSubjectName() {
		return subjectName;
	}

	public void setSubjectName(String subjectName) {
		this.subjectName = subjectName;
	}

	public int getStudentDNI() {
		return studentDNI;
	}

	public void setStudentDNI(int studentDNI) {
		this.studentDNI = studentDNI;
	}

	public Profesor getProfesor() {
		return profesor;
	}

	public void setProfesor(Profesor profesor) {
		this.profesor = profesor;
	}

	public List<Asignatura> getSubjectList() {
		return subjectList;
	}

	public void setSubjectList(List<Asignatura> subjectList) {
		this.subjectList = subjectList;
	}

	public List<Alumno> getStudentList() {
		return studentList;
	}

	public void setStudentList(List<Alumno> studentList) {
		this.studentList = studentList;
	}
}
